package com.haha.demo.controller;

import com.haha.demo.entity.User;
import com.haha.demo.service.intf.UserService;
import com.haha.demo.vo.PageVo;
import com.haha.demo.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/user/")
@CrossOrigin
@Api(tags = "实现用户相关")
public class UserController {
    @Resource
    private UserService service;

    @GetMapping("all.do")
    @ApiOperation(value = "查询全部",notes = "查询全部")
    public R all(){
        return service.all();
    }

    @GetMapping("page.do")
    @ApiOperation(value = "查询全部",notes = "查询全部")
    public PageVo page(int page, int limit){
        return service.querPage(page,limit);
    }

    @PostMapping("login.do")
    @ApiOperation(value = "登录接口",notes = "登录接口")
    public R page(String name, String pwd, HttpSession session){
        return service.login(name,pwd,session);
    }

    @GetMapping("detail.do")
    @ApiOperation(value = "获取用户详情",notes = "获取用户详情")
    public R detail(HttpSession session){
        return R.ok(session.getAttribute("user"));
    }

    @GetMapping("logout.do")
    @ApiOperation(value = "注销登录",notes = "注销登录")
    public R loginOut(HttpSession session){
        //注销session
        session.invalidate();
        return R.ok();
    }

    @PostMapping("edit.do")
    @ApiOperation(value = "编辑当前的用户",notes = "编辑当前的用户")
    public R edit(User user){
        return service.update(user);
    }

    @PostMapping("delete.do")
    @ApiOperation(value = "删除用户",notes = "删除用户")
    public R delete(int id){
        return service.delete(id);
    }

    @PostMapping("add.do")
    @ApiOperation(value = "添加用户",notes = "添加用户")
    public R add(User user){
        return service.add(user);
    }


}
